<?php
class Event_BlockController extends Clu_Controller_Action_Mini
{
	
	public function init()
	{
		//require_once (APP_PATH.'/modules/place/Model/Place.php');
		//require_once (APP_PATH.'/modules/article/Model/Article.php');
		//require_once (APP_PATH.'/modules/event/Model/Event.php');
		$this->mPlace = new place_Model_Place();
		$this->mArticle = new article_Model_Service();
		$this->m = new event_Model_Service();
		parent::init();
	}
	
	public function relatedPlaceAction()
	{
		$placeId = $this->miniRequest['placeId'];
		$regionId = $this->miniRequest['regionId'];
		$boxWeight = $this->miniRequest['boxWeight'];
		$this->view->placeId = $placeId;
		//todo:find events close to the place $placeId
		//truyen vao view
		$now = getdate(time());
  		$time = mktime(0,0,0, $now['mon'], 1, $now['year']);
  		$date = getdate($time);
  		$start_date= date("d/m/Y");
  		$finish_date = date("d/m/Y", $date['0'] + 7776000);
		$data = array('frequency' => 'monthly', 'startDate' => $start_date, 'finishDate' => $finish_date, 'place_id' => $placeId);
		$paginatorEvent = $this->getListEvent($data);
       	$this->view->paginator = $paginatorEvent;
//       	die_a($paginatorEvent->getCurrentItems());
		$this->view->listEvents = $paginatorEvent->getCurrentItems();
		$articleList = array();
		$mArticle = new article_Model_Service();
		foreach ($this->view->listEvents as $e)
		{
			$articleList[$e['id']] = $mArticle->getArticleData($e['article_id']);
		}
		$this->view->aList = $articleList;
//		die_a($this->view->listEvents);
		$this->renderToHolder('block/related-to-place.phtml', false, null, $regionId, $boxWeight);
	}
	
	public function getListEvent($data)
	{
		if(isset($data['item_per_page']) == null)
			$data['item_per_page'] = 10 ;
		if(isset($data['page']) == null)
			$data['page'] = 1 ;
	   	$select = $this->m->search($data);
	   	$paginator = $this->generatePaginator($select, $data['page'], $data['item_per_page'], 5, 'Jumping');
	   	$sess = new Zend_Session_Namespace("paginatorEvent");
	   	$sess->event_list = array("event_params" => $data);
        $data = $sess->event_list;
				
      	//cache in $_SESSION
        if(isset($data['page']))
        {
   			$select = $this->m->search($data['event_params']);
       		$paginator = $this->generatePaginator($select, $this->_getParam('page'), $data['item_per_page'],5, 'Jumping');
        }
        return $paginator;
	}
	
	public function generatePaginator($select, $pageNumber, $itemsPerPage, $pageRange, $style)
    {
		$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($select));
		$paginator->setItemCountPerPage($itemsPerPage);
		$paginator->setCurrentPageNumber($pageNumber);
		$paginator->setPageRange($pageRange);
		Zend_Paginator::setDefaultScrollingStyle($style);
		return $paginator;
    }
    
    public function relatedUserAction()
    {
    	$id = $this->miniRequest['id'];
		$regionId = $this->miniRequest['regionId'];
		$boxWeight = $this->miniRequest['boxWeight'];
		$this->view->id = $id;
		
		$data = array('poster_id' => $id,'item_per_page' => 5);
		$paginatorEvent = $this->getListEvent($data);
       	$this->view->paginator = $paginatorEvent;
		$this->view->listEvents = $paginatorEvent->getCurrentItems();
		$this->renderToHolder('block/related-to-user.phtml', false, null, $regionId, $boxWeight);
    }
    
    public function featuredListAction()
    {
//    	$id = $this->miniRequest['id'];
		$regionId = $this->miniRequest['regionId'];
		$boxWeight = $this->miniRequest['boxWeight'];

		$data = array('status' => 'featured','item_per_page' => 4);
		$paginatorEvent = $this->getListEvent($data);
       	$this->view->paginator = $paginatorEvent;
		$this->view->eventList =  $paginatorEvent->getCurrentItems();
		$articleList = array();
		$mArticle = new article_Model_Service();
		foreach ($this->view->eventList as $e)
		{
			$articleList[$e['id']] = $mArticle->getArticleData($e['article_id']);
		}
		$this->view->aList = $articleList;
		//die_a($this->view->aList);

		//die_a($this->view->eventList);
//    	if ( $this->miniRequest['page'] == 'home')
    		$this->renderToHolder('block/featured-event-list-home.phtml', false, null, $regionId, $boxWeight);
    }
    
 	public function simpleSearchAction()
    {
    	$regionId = $this->miniRequest['regionId'];
		$boxWeight = $this->miniRequest['boxWeight'];
    	$form = new event_Form_EventSearch();
        $form->cluBuild(array('action' => 'search', 'view' => $this->view, 'cluSearchMode' => 'simpleSearch'));
        $this->view->form = $form;
		$this->renderToHolder('block/simple-search.phtml', false, null, $regionId, $boxWeight);
    }
    
	public function browseByCategoryAction()
    {
		$regionId = $this->miniRequest['regionId'];
		$boxWeight = $this->miniRequest['boxWeight'];

		$this->view->category = array(11, 19, 20, 21, 22, 23, 24);
		$this->view->events_per_page = 5;
		$this->view->page = 1;
		$this->view->clu_search = 1;
		$this->renderToHolder('block/browse-by-categories.phtml', false, null, $regionId, $boxWeight);
    }
    
    public function upcomingEventAction()
    {
    	//$id = $this->miniRequest['id'];
		$regionId = $this->miniRequest['regionId'];
		$boxWeight = $this->miniRequest['boxWeight'];

		$data = array('status' => 'featured','item_per_page' => 4);
		$paginatorEvent = $this->getListEvent($data);
       	$this->view->paginator = $paginatorEvent;
		$this->view->eventList =  $paginatorEvent->getCurrentItems();
		$articleList = array();
		$mArticle = new article_Model_Service();
		foreach ($this->view->eventList as $e)
		{
			$articleList[$e['id']] = $mArticle->getArticleData($e['article_id']);
		}
		$this->view->aList = $articleList;

		//die_a($this->view->eventList);
   		$this->renderToHolder('block/upcoming-event.phtml', false, null, $regionId, $boxWeight);
    }
    
    public function listCommentAction()
    {
    	$regionId = $this->miniRequest['regionId'];
		$boxWeight = $this->miniRequest['boxWeight'];
		$this->renderToHolder('block/list-comment.phtml', false, null, $regionId, $boxWeight);
    }
    
    public function featuredListHomeAction()
    {
		$regionId = $this->miniRequest['regionId'];
		$boxWeight = $this->miniRequest['boxWeight'];
		$data = array('item_per_page' => 6,'status' => 'featured');
		$paginator = $this->getListEvent($data);
       	$this->view->paginator = $paginator;
       	$results = $paginator->getCurrentItems();
//		$hcm = $results[0];
//		$results[0] = $results[1]; // 0 = Dalat
//		$results[1] = $results[5]; //1 = Bat trang
//		$results[5] = $hcm;
//		unset($results[2]); //leave out Phuquoc
       	//gallery info for each place, and kept in a separate array 
       	$placeGalleries = array();
		foreach($results as $p)
		{
	       	$contentType = 'place';
			$id = $p['id'];
			$fileType = 'image'; 
			$userId = $p['poster_id'];
	       	if (!$contentType || !$id || !$fileType)
	       		return;
				//die("contentType or id or fileType is not specified. Die!!");
			//find the directory
			$dir = APP_PATH."/public/ufiles/$userId/images/original/$contentType/";
			if(file_exists($dir))
			{
			    $files = scandir($dir);
			    $displayFiles = array();
			    foreach ($files as $f)
			    {
			    	if (strpos ($f, $id) === 0)
			    		$displayFiles[] = $f;
			    }
			    $placeGalleries[$id]['displayFiles'] = $displayFiles;
			    $placeGalleries[$id]['base'] = "/public/ufiles/$userId/images/%s/$contentType/";
			}
		}
		
		$this->view->eventList = $results;
		
		$articleList = array();
		$mArticle = new article_Model_Service();
		foreach ($this->view->eventList as $p)
		{
			$articleList[$p['id']] = $mArticle->getArticleData($p['article_id']);
		}
		$this->view->aList = $articleList;
	
		$this->view->placeGalleries = $placeGalleries;
    	if ( $this->miniRequest['page'] == 'home')
    		$this->renderToHolder('block/event-list-home.phtml', false, null, $regionId, $boxWeight);
	}
	
}
?>